package com.puhui.lib.utils

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.text.Html
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.Window
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.puhui.lib.R

/**
 * 通用提示弹窗
 */
class AlertDialogUtils {

    private fun showDialog(context: Context, builder: Builder): AlertDialog? {
        val view = View.inflate(context, R.layout.alert_dialog_layout, null)
        val mAlertDialog = AlertDialog.Builder(context).create()
        mAlertDialog.setView(view)
        mAlertDialog.show()
        val window = mAlertDialog.window
        window?.let {
            it.setContentView(R.layout.alert_dialog_layout)
            //AlertDialog自定义界面圆角有背景问题    有白色背景，加这句代码
            it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

            //改变window的宽度，并不能改变布局的实际宽度，导致布局显示不全
            //应将根布局的layout_width属性设置为match_parent
            val params = it.attributes
            params.width = ScreenUtils.getScreenWidth() - SizeUtils.dp2px(60f)
            it.attributes = params

            //填充数据
            setViewData(it, builder, mAlertDialog)
        }
        mAlertDialog.setCancelable(builder.canCancelable)
        mAlertDialog.setCanceledOnTouchOutside(builder.canCancelable)
        return mAlertDialog
    }

    private fun setViewData(window: Window, builder: Builder, mAlertDialog: AlertDialog) {

        //title
        if (!TextUtils.isEmpty(builder.title)) {
            val titleTv = window.findViewById<TextView>(R.id.title_tv)
            titleTv.visibility = View.VISIBLE
            titleTv.text = builder.title
        }

        //提示内容
        val contentView = window.findViewById<TextView>(R.id.content)
        if (builder.isContentHtml) {
            contentView.text = Html.fromHtml(builder.tipContent)
        } else {
            contentView.text = builder.tipContent
        }
        if (builder.isCenter) {
            contentView.gravity = Gravity.CENTER
        }
        if (builder.contentColor != 0) {
            contentView.setTextColor(getColor(window, builder.contentColor))
        }

        //确认按钮
        val btnOk = window.findViewById<TextView>(R.id.btn_ok)
        if (!TextUtils.isEmpty(builder.confirmStr)) {
            btnOk.text = builder.confirmStr
        }
        if (builder.confirmTextColor != 0) {
            btnOk.setTextColor(getColor(window, builder.confirmTextColor))
        }
        btnOk.setOnClickListener {
            builder.onConfirmListener?.onConfirm()
            mAlertDialog.dismiss()
        }

        //取消按钮
        val btnCancel = window.findViewById<TextView>(R.id.btn_cancel)
        if (!TextUtils.isEmpty(builder.cancelStr)) {
            btnCancel.text = builder.cancelStr
        }
        if (builder.cancelTextColor != 0) {
            btnCancel.setTextColor(getColor(window, builder.cancelTextColor))
        }
        btnCancel.setOnClickListener {
            builder.onCancelListener?.onCancel()
            mAlertDialog.dismiss()
        }
        if (builder.isShowOne) {
            btnCancel.visibility = View.GONE
            window.findViewById<View>(R.id.line_between_confirm_cancel).visibility = View.GONE
        }
    }

    private fun getColor(window: Window, colorResId: Int): Int {
        return ContextCompat.getColor(window.context, colorResId)
    }

    interface OnCancelListener {
        fun onCancel()
    }

    interface OnConfirmListener {
        fun onConfirm()
    }

    class Builder {
        var title: String? = null
        var confirmStr: String? = null
        var cancelStr: String? = null
        var tipContent: String? = null
        var contentColor = 0
        var confirmTextColor = 0
        var cancelTextColor = 0
        var isCenter = true //提示内容是否居中
        var isShowOne = false //是否只显示确定按钮
        var isContentHtml = false;  //提示内容是否是HTML
        var onCancelListener: OnCancelListener? = null
        var onConfirmListener: OnConfirmListener? = null
        var canCancelable = true //弹窗是否可点击其他区域消失

        companion object {
            fun newInstance(): Builder {
                return Builder()
            }
        }

        fun setCenter(center: Boolean): Builder {
            isCenter = center
            return this
        }

        fun setTitle(title: String?): Builder {
            this.title = title
            return this
        }

        fun setConfirmStr(confirmStr: String?): Builder {
            this.confirmStr = confirmStr
            return this
        }

        fun setCancelStr(cancelStr: String?): Builder {
            this.cancelStr = cancelStr
            return this
        }

        fun isContentHtml(isContentHtml: Boolean): Builder {
            this.isContentHtml = isContentHtml
            return this
        }

        fun setContentColor(@ColorRes contentColor: Int): Builder {
            this.contentColor = contentColor
            return this
        }

        fun confirmTextColor(@ColorRes confirmTextColor: Int): Builder {
            this.confirmTextColor = confirmTextColor
            return this
        }

        fun cancelTextColor(@ColorRes cancelTextColor: Int): Builder {
            this.cancelTextColor = cancelTextColor
            return this
        }

        fun isShowOne(showOne: Boolean): Builder {
            isShowOne = showOne
            return this
        }

        fun setTipContent(tipContent: String?): Builder {
            this.tipContent = tipContent
            return this
        }

        fun setOnCancelListener(onCancelListener: OnCancelListener?): Builder {
            this.onCancelListener = onCancelListener
            return this
        }

        fun setOnConfirmListener(onConfirmListener: OnConfirmListener?): Builder {
            this.onConfirmListener = onConfirmListener
            return this
        }

        fun setCanCancelable(canCancelable: Boolean): Builder {
            this.canCancelable = canCancelable
            return this
        }

        fun show(context: Context): AlertDialog? {
            return build().showDialog(context, this)
        }

        private fun build(): AlertDialogUtils {
            return AlertDialogUtils()
        }

    }
}